package cn.alise.qdp.service.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import cn.alise.qdp.mapper.admin.OrganMapper;
import cn.alise.qdp.model.common.OrganTree;
import cn.alise.qdp.model.organ.Organ;

@Service
@Transactional(rollbackFor = Exception.class)
public class OrganService {

	@Resource(name = "organMapper")
	private OrganMapper organMapper;
	
	public Organ get(Organ args) {
		List<Organ> list = this.list(args);
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		
		return null;
	}
	
	public Organ getParent(Organ args) {
		List<Organ> list = this.getParentByOrganId(args);
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		
		return null;
	}
	
	public List<Organ> gets(Map<String, Object> args){
		return this.organMapper.gets(args);
	}
	
	public List<Organ> list(Organ args) {
		return this.organMapper.list(args);
	}
	
	public List<Organ> listTree(Organ args) {
		return this.organMapper.listTree(args);
	}
	
	public List<Organ> page(Map<String, Object> args) {
		return this.organMapper.page(args);
	}
	
	public int total(Organ args) {
		return this.organMapper.total(args);
	}
	
	public int delete(Organ args) {
		return this.organMapper.delete(args);
	}
	
	public int save(Organ args) {
		return this.organMapper.insert(args);
	}
	
	public int update(Organ args) {
		return this.organMapper.update(args);
	}
	
	public List<Organ> getOneList(Organ args) {
		return this.organMapper.getOneList(args);
	}
	
	public List<Organ> getTwoList(Organ args) {
		return this.organMapper.getTwoList(args);
	}
	
	public List<Organ> getThreeList(Organ args) {
		return this.organMapper.getThreeList(args);
	}
	
	public List<Organ> getParentList(Organ args) {
		return this.organMapper.getParentList(args);
	}
	
	public List<Organ> getParentByOrganId(Organ args) {
		return this.organMapper.getParentByOrganId(args);
	}
	
	/** 
	 * 递归设置机构树(tree展示)
	 * @return 
	 */  
	public List<OrganTree> createComboTreeChildren(List<Organ> list, String fid) {  
		List<OrganTree> childList = new ArrayList<OrganTree>();  
	    for (int j = 0; j < list.size(); j++) {  
	        Organ treeChild = (Organ) list.get(j); 
	        if (treeChild.getParentId() != null) {
	        	if (treeChild.getParentId().toString().equals(fid)) {
		            OrganTree tree = new OrganTree();
		            tree.setId(treeChild.getOrganId());
		            tree.setText(treeChild.getOrganName());
					if(treeChild.getParentId()!=null) {
						tree.setPid(treeChild.getParentId().toString());
					} else {
						tree.setPid("");
					}
					
					List<OrganTree> clist = createComboTreeChildren(list, treeChild.getOrganId().toString());
					tree.setChildren(clist);
					childList.add(tree);
		        } 
	        }
	         
	    }  
	    return childList;  
	} 
	
}
